package com.link.base.tencent.corpwx.appchat.controller;

import com.link.base.tencent.corpwx.appchat.model.AppChatUser;
import com.link.base.tencent.corpwx.appchat.service.AppChatUserService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应用群聊-成员 
 *
 * @author 欧阳振强
 * @date 2020-01-03 10:41:04.0
 */
@Controller
@RequestMapping("/link/corpwx/appChatUser")
public class AppChatUserController extends BasicController<AppChatUser> {

    @Resource
    private AppChatUserService appChatUserService;

    @Override
    public BasicService<AppChatUser> getBasicService() throws Exception {
        return appChatUserService;
    }     
    
    /**
     * 加入群聊成员
     * 
     * @author 欧阳振强
     * @date 2020/01/05
     */
    @PostMapping(value = "/addAppChatUser", name = "加入群聊成员")
    @ResponseBody
    public Map<String, Object> addAppChatUser(@JsonParam AppChatUser record, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            appChatUserService.addAppChatUser(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 删除群聊成员
     *
     * @author 欧阳振强
     * @date 2020/01/05
     */
    @PostMapping(value = "/delAppChatUser", name = "删除群聊成员")
    @ResponseBody
    public Map<String, Object> delAppChatUser(@JsonParam AppChatUser record, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            appChatUserService.delAppChatUser(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 选择群聊成员
     *
     * @author ouyangzhenqiang
     * @date 2019/07/25 14:00
     */
    @PostMapping(value = "/queryUnSelectUserListPage", name = "选择群聊成员")
    @ResponseBody
    public Map<String, Object> queryUnSelectUserListPage(@JsonParam AppChatUser record, @JsonParam QueryParams qps,
            HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            AppChatUser t = BasicModel.transformClass(AppChatUser.class, qps);
            t.setHeadId(record.getHeadId());
            List<AppChatUser> list = appChatUserService.queryUnSelectUserListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<AppChatUser>());
            result.put("result", e.getMessage());
            if (e instanceof BasicServiceException) {
                result.put("detailMessage", ((BasicServiceException) e).getDetailMessage());
            }
        }
        return result;
    }
}